<?php
	App::import('Vendor', 'fpdf/fpdf');
	App::import('Vendor', 'fpdf/fpdf_graph');
	App::import('Vendor', 'fpdf/fpdf_table');
	
	/** FPDF Report Helper - Version 1.0.20090717
	 * Create PDF Reports, using FPDF library.
	 * http://www.fpdf.org
	 * http://code.google.com/p/fpdfreport
	 * 
	 * @author Iván Bustamante
	 * @modified 7 July 2009 by Iván Bustamante
	 * @category CakePHP Helper
	 * @license GPL v3
	 * @version 1.0.20090717
	 *  
	 **/
	 
	class FpdfReportHelper extends AppHelper {
		/**
		 * The FPDF object
		 *
		 * @var object
		 **/
		public $FPDF;
		
		/**
		 * The orientation for the PDF pages.
		 * Possible values:
		 * 'P' Portrait
		 * 'L' Landscape
		 *
		 * @var string
		 **/
		private $pdfOrientation = 'P';
		
		/**
		 * The unit for the PDF pages.
		 * Possible values:
		 * 'pt' Points
		 * 'mm' Millimeter
		 * 'cm' Centimeter
		 * 'in' Inch
		 *
		 * @var string
		 **/
		private $pdfUnit = 'pt';
		
		/**
		 * The page size for the PDF pages.
		 * Possible values:
		 * 'Letter'
		 * 'Legal'
		 * 'A3'
		 * 'A4'
		 * 'A5'
		 *
		 * @var string
		 **/
		private $pdfFormat = 'Letter';
		
		/**
		 * The graph list to be drawn on a page.
		 *
		 * @var array
		 **/
		private $graphs = array();
		
		/**
		 * The table list to be drawn on a page.
		 *
		 * @var array
		 **/
		private $tables = array();
		
		/**
		 * The PDF file name.
		 *
		 * @var string
		 **/
		private $fileName = "report.pdf";
		
		/**
		 * The PDF file destination.
		 * Possible values:
		 * 'I' Send inline to browser
		 * 'D' Send to browser and force file download
		 * 'F' Save locally
		 * 'S' Return document as a string
		 *
		 * @var string
		 **/
		private $fileDestination = "I";
		
		/**
		 * The header title.
		 *
		 * @var string
		 **/
		private $headerTitle = "FPDF Reports";
		
		/**
		 * The header subtitle.
		 *
		 * @var string
		 **/
		private $headerSubtitle = "Subtitle";
		
		/**
		 * The front page title.
		 *
		 * @var string
		 **/
		private $title = "FPDF Report Title";
		
		/**
		 * The front page subtitle.
		 *
		 * @var string
		 **/
		private $subtitle = "Subtitle";
		
		/**
		 * The footer page.
		 *
		 * @var string
		 **/
		private $footerText = "Page ";
		
		/**
		 * The path for a logo to display on the reports.
		 *
		 * @var string
		 **/
		private $logoPath = "logo.jpg";
		
		/**
		 * If true, footers are printed on the report pages.
		 * Else, no footers are printed.
		 *
		 * @var string
		 **/
		private $footerEnabled = true;
		
		/**
		 * If true, header are printed on the report pages.
		 * Else, no header are printed.
		 *
		 * @var string
		 **/
		private $headerEnabled = true;
		
		/**
		 * If true, a front page is printed.
		 * Else, it's not printed.
		 *
		 * @var string
		 **/
		private $frontPageEnabled = true;
		
		/**
		 * Creates a new FPDF Graph
		 *
		 * @return object A new FPDFGraph object
		 **/
		public function createGraph(){
			var $graph = new FPDFGraph();
			return $graph;
		}
		
		/**
		 * Creates a new FPDF Table
		 *
		 * @return object A new FPDFTable object
		 **/
		public function createTable(){
			var $table = new FPDFTable();
			return $table;
		}
		
		/**
		 * Set the parameters for the PDF page format.
		 * Use before beginPDF().
		 *
		 * @param string $orientation Page orientation
		 * @param string $unit PDF working units
		 * @param string $format Page size
		 **/ 
		public function setupPDF($orientation, $unit, $format){
			$this->pdfOrientation = $orientation;
			$this->pdfUnit = $unit;
			$this->pdfFormat = $format;
		}
		
		/**
		 * Begin the PDF file.
		 **/ 
		public function beginPDF(){
			$this->FPDF = new FPDF($this->pdfOrientation, $this->pdfUnit, $this->pdfFormat);
			$this->FPDF->SetAutoPageBreak(true, 20);
			$this->FPDF->SetTopMargin(0);
			$this->FPDF->AliasNbPages();
			
			if($this->frontPageEnabled)
				$this->frontPage();
		}
		
		/**
		 * Add a graph to the current page
		 *
		 * @param object $graph The graph to be added
		 **/
		public function addGraph($graph){
			$count = count($this->graphs);
			$this->graphs[$count] = $graph;
		}
		
		/**
		 * Add a table to the current page
		 *
		 * @param object $table The table to be added
		 **/
		public function addTable($table){
			$count = count($this->tables);
			$this->tables[$count] = $table;
		}
		
		/**
		 * Print current page
		 **/
		public function printPage(){
			$this->FPDF->AddPage();
			
			if($this->headerEnabled)
				$this->header();
			
			foreach($this->graphs as $graph)
				$graph->draw($this->FPDF);
			foreach($this->tables as $table)
				$table->draw($this->FPDF);
			$this->tables = array();
			$this->graphs = array();
			
			if($this->footerEnabled)
				$this->footer();
		}
		
		/**
		 * End the PDF document
		 **/
		public function endPDF(){
			$this->FPDF->Output($this->fileName, $this->fileDestination);
		}
		
		/**
		 * Change the target PDF file name
		 *
		 * @param string $fileName The file name
		 **/
		public function setFileName($fileName){
			$this->fileName = $fileName;			
		}
		
		/**
		 * Change the PDF file destination
		 *
		 * @param string $fileDestination The file destination
		 **/
		public function setFileDestination($fileDestination){
			$this->fileDestination = $fileDestination;			
		}
		
		/**
		 * Set the header's title and subtitle
		 *
		 * @param string $title
		 * @param string $subtitle
		 **/
		public function setHeader($title, $subtitle){
			$this->headerTitle = $title;
			$this->headerSubtitle = $subtitle;
		}
		
		/**
		 * Set the footer's text
		 *
		 * @param string $text
		 **/
		public function setFooter($text){
			$this->footerText = $text;
		}
		
		/**
		 * Set the path for the logo to be used throughout the document
		 *
		 * @param string $path The path to the image
		 **/
		public function setLogo($path){
			$this->logoPath = $path;
		}
		
		/**
		 * Set the front page's title and subtitle
		 *
		 * @param string $title
		 * @param string $subtitle
		 **/
		public function setFrontPage($title, $subtitle){
			$this->title = $title;
			$this->subtitle = $subtitle;
		}
		
		/**
		 * Disable the header for the document
		 *
		 **/
		public function disableHeader(){
			$this->headerEnabled = false;
		}
		
		/**
		 * Disable the front page for the document
		 *
		 **/
		public function disableFrontPage(){
			$this->frontPageEnabled = false;
		}
		
		/**
		 * Disable the footer for the document
		 *
		 **/
		public function disableFooter(){
			$this->footerEnabled = false;
		}
		
		/**
		 * Enable the header for the document
		 *
		 **/
		public function enableHeader(){
			$this->headerEnabled = true;
		}
		
		/**
		 * Enable the front page for the document
		 *
		 **/
		public function enableFrontPage(){
			$this->frontPageEnabled = true;
		}
		
		/**
		 * Disable the footer for the document
		 *
		 **/
		public function enableFooter(){
			$this->footerEnabled = true;
		}
		
		/**
		 * Prints a front page
		 *
		 **/
		private function frontPage(){
			$this->FPDF->AddPage();
			
			if($this->logoPath != "")
					$this->FPDF->Image($this->logoPath, 30, 30, 200, 80);
			
			$this->FPDF->SetXY(0,350);
			$this->FPDF->SetFont('Arial','B',20);
			$this->FPDF->SetTextColor(0,0,0);
			$this->FPDF->Cell(0, 20, $this->title, 0, 1, "C");
			
			$this->FPDF->SetFont('Arial','',16);
			$this->FPDF->SetTextColor(50,50,50);
			$this->FPDF->Cell(0, 16, $this->subtitle, 0, 0, "C");
		}
		
		/**
		 * Prints a header on the current page
		 *
		 **/
		private function header(){
			if($this->FPDF->PageNo() == 1){
			}
			else{
				$this->FPDF->SetXY(0,45);
				$this->FPDF->SetFont('Arial','',10);
				$this->FPDF->SetTextColor(100,100,100);
				$this->FPDF->Cell(0, 10, $this->headerTitle, 0, 1, "R");
				
				$this->FPDF->SetFont('Arial','',8);
				$this->FPDF->SetTextColor(150,150,150);
				$this->FPDF->Cell(0, 8, $this->headerSubtitle, 0, 0, "R");
				
				if($this->logoPath != "")
					$this->FPDF->Image($this->logoPath, 30, 30, 100, 40);
			}
		}
		
		/**
		 * Prints a footer on the current page
		 *
		 **/
		private function footer(){
			if($this->FPDF->PageNo() == 1){
			}
			else{
				$this->FPDF->SetXY(1,-60);
				$this->FPDF->SetFont('Arial','I',8);
				$this->FPDF->SetTextColor(150,150,150);
				$this->FPDF->Cell(0, 8, $this->footerText.$this->FPDF->PageNo()." / {nb}", 0, 0, 'C');
			}
		}
	}
?>